<?php
namespace app\config;
use Core\Common\Common;
use Core\Result\Result;

class Db {
    public $conn;
    public function __construct($type = 'follow'){
        switch ($type){
            case 'host':
                $this->host_mysql();
                break;
            default:
                $this->follow_mysql();
        }
    }

    /**
     * 主库更新插入
     * @return false|string
     */
    private function host_mysql(){
        $databaseInfo = Common::config('database_config');
        $this->conn = mysqli_connect($databaseInfo['host_database']['host'],$databaseInfo['host_database']['user'],$databaseInfo['host_database']['password'],$databaseInfo['host_database']['database']);
        if (!$this->conn)
        {
            Result::error('Could not connect: ' . mysqli_error());
        }
        mysqli_query($this->conn,"set names 'utf8'");

    }

    /**
     * 从库查询
     * @return false|string
     */
    private function follow_mysql(){
        $databaseInfo = Common::config('database_config');
        $randId = array_rand($databaseInfo['follow_database'],1);
        $this->conn = mysqli_connect($databaseInfo['follow_database'][$randId]['host'],$databaseInfo['follow_database'][$randId]['user'],$databaseInfo['follow_database'][$randId]['password'],$databaseInfo['follow_database'][$randId]['database']);
        if (!$this->conn)
        {
            Result::error('Could not connect: ' . mysqli_error());
        }
        mysqli_query($this->conn,"set names 'utf8'");
    }

    /**
     * 关闭数据库连接
     */
    private function close()
    {
        mysqli_close($this->conn);
    }

    /**
     * 查询一条数据
     * @param $query
     * @return string[]|null
     */
    public function find($query){
        $data = [];
        $res = mysqli_query($this->conn, $query);
        if($res) $data = mysqli_fetch_assoc($res);
        return $data;
    }

    /**
     * 查询多条数据
     * @param $query
     * @return array
     */
    public function query($query,$key=''){
        $data = [];
        $res = mysqli_query($this->conn, $query);
        if($res){
            if(empty($key)){
                while ($rs = mysqli_fetch_assoc($res)){
                    $data[] = $rs;
                }
            }else{
                while ($rs = mysqli_fetch_assoc($res)){
                    $data[$rs[$key]] = $rs;
                }
            }
        }
        return $data;
    }

    /**
     * 数据插入更新
     * @param $query
     * @return bool
     */
    public function save($query){
        $res = mysqli_query($this->conn, $query);
        $rs = mysqli_affected_rows($this->conn);
        if($res == false || $rs == 0){
            return false;
        }
        return true;
    }
    /**
     * 返回上次插入的id
     * @return int
     * */
    public function mysql_insert_id(){
        return mysqli_insert_id($this->conn);
    }
    /**
     * 开始事务
     */
    public function startTrans(){
        mysqli_autocommit($this->conn,false);
    }

    /**
     * 事务回滚
     */
    public function rollback(){
        mysqli_rollback($this->conn);
    }

    /**
     * 事务提交
     */
    public function commit(){
        mysqli_commit($this->conn);
    }
}